/*
 * Copyright (C) 2022-2024, Xiaohua Semiconductor Co., Ltd.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-04-28     CDT          first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include "led.h"
#include "cpuHc32f460.h"
#include "MQ.h"
#include "hc32_ll_fcg.h"
#include "hc32_ll_utility.h"
#include "hc32_ll_rmu.h"
#include "power.h"
#include "RTC_8025T.h"
#include "RN8306.h"
#include "Flash.h"
#include "EEPROM.h"
#include "anv_leaks.h"
#include <rtthread.h>
#include <dfs_fs.h>
#include "easyflash.h"
#include "ulog_easyflash.h"
#include "ulog_be.h"

#define LOG_TAG     "main"
#define DBG_LVL     DBG_LOG
#include "ulog.h"

static void Thread_Task()
{
    rt_thread_t tid[20];
   
    /*电源管理*/
    tid[0] = rt_thread_create("power", power_thread_entry, RT_NULL, RT_MAIN_THREAD_STACK_SIZE, 15,100);
    rt_thread_startup(tid[0]);
     
    /*LED管理*/
    tid[1] = rt_thread_create("led",led_thread_entry, RT_NULL,RT_MAIN_THREAD_STACK_SIZE,14, 100);
    rt_thread_startup(tid[1]);
    
    rt_thread_mdelay(1000);
    /*UART管理*/
    
    /*SPI_8306通信*/
    tid[2] = rt_thread_create("RN8306", RN8306_thread_entry, RT_NULL, RT_MAIN_THREAD_STACK_SIZE, 12,100);
    rt_thread_startup(tid[2]);
    /*IIC_8025T通信*/
    tid[3] = rt_thread_create("8025T", RTC_8025T_thread_entry, RT_NULL, RT_MAIN_THREAD_STACK_SIZE, 13,100);
    rt_thread_startup(tid[3]);
    
    /*SPI_8306_DMA通信,优先级最低*/
    tid[4] = rt_thread_create("RN8306dma", RN8306_DMA_thread_entry, RT_NULL, RT_MAIN_THREAD_STACK_SIZE, 16,100);
    rt_thread_startup(tid[4]);
    
    /*SPI_FLash通信*/
    tid[5] = rt_thread_create("Flash", Flash_thread_entry, RT_NULL, RT_MAIN_THREAD_STACK_SIZE, 10,100);
    rt_thread_startup(tid[5]);
    
    /*IIC_EEPROM*/
    tid[6] = rt_thread_create("EEPROM", EEPROM_thread_entry, RT_NULL, RT_MAIN_THREAD_STACK_SIZE, 11,100);
    rt_thread_startup(tid[6]); 
    
    rt_thread_mdelay(2000);
    
    //给芯片的RTC赋值
    set_date(RealTimer.wYear, RealTimer.Mon, RealTimer.Day);
    set_time(RealTimer.Hour, RealTimer.Min, RealTimer.Sec);
}



    
void ulog_file_init(void )
{
//ulog_flash后端
#ifdef PKG_USING_EASYFLASH
    easyflash_init();
    ulog_ef_filter_cfg_load();
    ulog_ef_backend_init();
    ulog_ef_log_lvl_set(LOG_LVL_DBG);
#endif
    
#ifdef ULOG_BACKEND_USING_FILE
    struct ulog_file_be *file_be = &sys_log_file; 
    uint8_t id = 0;  
    file_be->parent = sys_log_backend;  
//    ulog_backend_filter_t filter = motion_log_file_backend_filter;  //过滤函数，目前用不到
    ulog_file_backend_init( file_be, table[id].name, table[id].dir_path, table[id].max_num, table[id].max_size, table[id].buf_size);
    ulog_file_backend_enable(file_be);
#endif
}
int main(void)
{
    //开中断
    ENABLE_CPU_INT;
    
    RMU_ClearStatus();//清复位标注

    anv_leaks_init(stdout);//内存管理初始化
    
    mq_init();//消息队列
    
    Thread_Task();//多任务
    
    filesystem_mount();//挂载文件系统
    
    ulog_file_init();

    rt_kprintf("RT-thread V2.0!\n");

    LOG_D("ulog write flash Start");

    while (1)
    {
        rt_thread_mdelay(1000);
    }
}

